﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using Viewer.BusinessObject.BusinessEntities;
using BO = Viewer.BusinessObject.BusinessEntities;
namespace Viewer.NhanSu.UserControl
{
    public partial class UcToChuc : System.Web.UI.UserControl
    {
        BO.NSToChuc query = new Viewer.BusinessObject.BusinessEntities.NSToChuc();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadDataForTree();
            }
        }
        public void ReCallLoadData()
        {
            LoadDataForTree();
        }
        [DirectMethod]
        public void SetTypeButton(string ID)
        {
            int id;
            try
            {
                id = int.Parse(ID);
            }
            catch (Exception)
            {

                id = 0;
            }
            //if (id > 0 || !string.IsNullOrEmpty(txtCoQuanKhac.Text))
            //{
            //    BtnSelectToChuc.Disabled = false;
            //}
            //else
            //{
            //    BtnSelectToChuc.Disabled = true;
            //}
        }
        public void ShowLookUp_ToChuc(string mode, bool flag, string tochucKhac)
        {
            if (mode.Equals("ToChucKhac"))
            {
                txtCoQuanKhac.Disabled = false;
                cpToChucKhac.Hidden = false;

                if (!flag)
                {
                    txtCoQuanKhac.Text = tochucKhac;
                    cbbTypeToChuc.SelectedIndex = 1;
                    treePanel.Disabled = true;
                    txtCoQuanKhac.Disabled = false;
                }
                else
                {
                    txtCoQuanKhac.Text = "";
                    cbbTypeToChuc.SelectedIndex = 0;
                    treePanel.Disabled = false;
                    txtCoQuanKhac.Disabled = true;
                }
            }
            else
            {
                cbbTypeToChuc.SelectedIndex = 0;
                cpToChucKhac.Hidden = true;
            }



            wdToChuc.Show();
        }
        [DirectMethod]
        public void LoadDataForTree()
        {
            //Load Root                     
            Ext.Net.TreeNode root = new Ext.Net.TreeNode("Tổ Chức Hành Chính");
            root.Expanded = true;
            root.Disabled = true;
            root.CustomAttributes.Add(new ConfigItem("IsView", "0"));//disable

            treePanel.Root.Add(root);
            List<NSToChuc> lstToChuc = query.GetAll();
            List<NSToChuc> lstToChucLv1 = lstToChuc.Where(c => c.ToChucLevel == 1).ToList();

            foreach (var oToChucLv1 in lstToChucLv1)
            {
                var nodeLv1 = CreateNode(oToChucLv1);
                root.Nodes.Add(nodeLv1);
                List<NSToChuc> lstToChucLv2 = lstToChuc.Where(c => c.ToChucIDCha == oToChucLv1.ToChucID).ToList();
                foreach (var oToChucLv2 in lstToChucLv2)
                {
                    var nodeLv2 = CreateNode(oToChucLv2);
                    nodeLv1.Nodes.Add(nodeLv2);
                    List<NSToChuc> lstToChucLv3 = lstToChuc.Where(c => c.ToChucIDCha == oToChucLv2.ToChucID).ToList();
                    foreach (var oToChucLv3 in lstToChucLv3)
                    {
                        var nodeLv3 = CreateNode(oToChucLv3);
                        nodeLv2.Nodes.Add(nodeLv3);
                    }
                }
            }
            if (X.IsAjaxRequest)
            {
                Ext.Net.X.AddScript("refreshTree({0},{1});", treePanel.ClientID, treePanel.Root.ToJson());
            }
        }
        private List<QUNhomQuyenByToChuc_ToChuc> _nhomquyen;
        public List<QUNhomQuyenByToChuc_ToChuc> NhomQuyenByToChuc_ToChuc
        {
            get
            {
                if (_nhomquyen == null)
                {
                    //var oNguoidung = QUNguoiDung.GetByUserName(Page.User.Identity.Name.ToString());
                    //_nhomquyen = oNguoidung.NhomQuyenByToChuc_ToChuc();
                    _nhomquyen = (List<QUNhomQuyenByToChuc_ToChuc>)Session["NhomQuyenByToChuc_ToChuc"];
                }
                return _nhomquyen;
            }
        }
        private Ext.Net.TreeNode CreateNode(NSToChuc oToChuc)
        {
            //1 nguoi thuoc nhieu nhom quyen
            if (this.NhomQuyenByToChuc_ToChuc == null)
            {
                if (Context.User.Identity.Name.ToLower() != "admin")
                {
                    return CreateDisableNode(oToChuc.ToChucID.ToString(), oToChuc.TenToChucFull);
                }
            }
            //var lstNhomQuyenByToChuc_ToChuc = (from m in this.NhomQuyenByToChuc_ToChuc
            //                                   where m.ToChucID == oToChuc.ToChucID
            //                                   select m).ToList();
            //if (lstNhomQuyenByToChuc_ToChuc.Count == 0)
            //{
            //    //khong co quyen doi voi to chuc nay
            //    if (Context.User.Identity.Name.ToLower() != "admin")
            //    {
            //        return CreateDisableNode(oToChuc.ToChucID.ToString(), oToChuc.TenToChucFull);
            //    }
            //}
            //bool IsView = false;
            //bool IsAdd = false;
            //bool IsEdit = false;
            //bool IsDelete = false;

            //foreach (var item in lstNhomQuyenByToChuc_ToChuc)
            //{
            //    IsView = IsView || item.IsView;
            //    IsAdd = IsAdd || item.IsAdd;
            //    IsEdit = IsEdit || item.IsEdit;
            //    IsDelete = IsDelete || item.IsDelete;

            //}

            ////Hard code for admin 
            //if (Context.User.Identity.Name.ToLower() == "admin")
            //{
            //    IsView = true;
            //    IsAdd = true;
            //    IsEdit = true;
            //    IsDelete = true;
            //}

            ///////////////////
            //if (Context.User.Identity.Name.ToLower() != "admin")
            //{
            //    if (!IsView)
            //    {
            //        return CreateDisableNode(oToChuc.ToChucID.ToString(), oToChuc.TenToChucFull);
            //    }
            //}

            Ext.Net.TreeNode node = new Ext.Net.TreeNode(oToChuc.ToChucID.ToString(), oToChuc.TenToChucFull, Icon.Group);
            //node.CustomAttributes.Add(new ConfigItem("IsView", IsView ? "1" : "0"));
            //node.CustomAttributes.Add(new ConfigItem("IsAdd", IsAdd ? "1" : "0"));
            //node.CustomAttributes.Add(new ConfigItem("IsEdit", IsEdit ? "1" : "0"));
            //node.CustomAttributes.Add(new ConfigItem("IsDelete", IsDelete ? "1" : "0"));

            node.CustomAttributes.Add(new ConfigItem("IsView", "1"));
            node.CustomAttributes.Add(new ConfigItem("IsAdd", "1"));
            node.CustomAttributes.Add(new ConfigItem("IsEdit", "1"));
            node.CustomAttributes.Add(new ConfigItem("IsDelete", "1"));

            node.Expanded = true;
            return node;
        }

        /// <summary>
        /// Create a disable treenode
        /// </summary>
        /// <param name="id"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        private Ext.Net.TreeNode CreateDisableNode(string id, string text)
        {
            Ext.Net.TreeNode node = new Ext.Net.TreeNode(id + "Decline", text, Icon.GroupKey);
            node.CustomAttributes.Add(new ConfigItem("IsView", "0"));
            node.Disabled = true;
            node.Cls = "Disable";
            node.Expanded = true;
            return node;
        }
        protected void SelectType(object sender, EventArgs e)
        {
            if (cbbTypeToChuc.SelectedItem.Value.Equals("0"))
            {
                txtCoQuanKhac.Disabled = false;
            }
            else { txtCoQuanKhac.Disabled = true; }
        }
    }
}